package com.lihepeng.leecode.juc;

/**
 * 被 volatile 修饰的变量 是可以读取被其他线程修改的内容的
 */
class Resouce{
    private  int val;
    public void set60(){
        this.val  =60;
    }

    public int getVal() {
        return val;
    }

}
public class VolatileDemo {
    public static void main(String[] args) throws InterruptedException {
        Resouce resouce = new Resouce();
        Thread thread = new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"is comming");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resouce.set60();
            System.out.println("修改完成之后的值为："+resouce.getVal());
        },"AA");
        thread.start();
        while (resouce.getVal() ==0) {
        }
        System.out.println("完成任务 主线程已经读到子线程修改的变量的值");
    }
}
